import { defineStore } from 'pinia';
import request from '/@/assets/js/request';
import util from '/@/assets/js/util';

export const useOptionData = defineStore('optionDataStore', {
	state: () => ({
		optionData: {} as any,
		evaluateInfo: {} as object,
	}),
	// getters:{
	// 	optionData: (state) => {
	// 		return state.optionData
	// 	}
	// },
	actions: {
		getDictList(type:any){
			// 如果store中有这个对象，直接返回，否则用通过接口返回，并存入store
			return new Promise(async (resolve, reject) => {
				let list:any = [];
				if (JSON.stringify(this.optionData) != '{}') {
					if (this.optionData.hasOwnProperty(type)) { 
						list = this.optionData[type];
					} else {
						let res:any = await request('get', '/admin/dict/type/' + type, {});
						if (res.length > 0) {
							list = util.transformArray(res);
							this.optionData[type] = list;
						}
					}
				} else {
					let res:any = await request('get', '/admin/dict/type/' + type, {});
					if (res.length > 0) {
						list = util.transformArray(res);
						this.optionData[type] = list;
					}
				}			
				resolve(list);
			});
		},
		getEvaluateInfo(obj:any){
			this.evaluateInfo = obj;
		}
	},
});
